package com.atguigu.web;

import com.atguigu.pojo.*;
import com.atguigu.service.*;
import com.atguigu.service.impl.*;
import com.atguigu.utils.WebUtils;
import com.google.gson.Gson;
import org.apache.commons.lang3.StringUtils;


import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.lang.reflect.Array;
import java.util.*;


//前端访问这个对象,使用action,抽取数据,打包进service对象,把service对象存入serviceService类,存入数据库,返回"服务器首页"
public class ServiceServlet extends BaseServlet {
    ServerService serverService=new ServerServiceImpl();
    ServiceService serviceService=new ServiceServiceImpl();
    NginxServiceConfigProxyService nginxServiceConfigProxyService=new NginxServiceConfigProxyServiceImpl();
    NginxServiceConfigPhpService nginxServiceConfigPhpService=new NginxServiceConfigPhpServiceImpl();
    NginxServiceConfigTcpProxyService nginxServiceConfigTcpProxyService=new NginxServiceConfigTcpProxyServiceImpl();
    RedisServiceConfigService redisServiceConfigService=new RedisServiceConfigServiceImpl();
    MysqlServiceConfigService mysqlServiceConfigService=new MysqlServiceConfigServiceImpl();
    MongoServiceConfigService mongoServiceConfigService=new MongoServiceConfigServiceImpl();
    DockerServiceConfigService dockerServiceConfigService=new DockerServiceConfigServiceImpl();
    TomcatServiceConfigService tomcatServiceConfigService=new TomcatServiceConfigServiceImpl();
    ProjectService projectService=new ProjectServiceImpl();
    ProjectModuleService projectModuleService=new ProjectModuleServiceImpl();
    ProjectModuleTypeService projectModuleTypeService=new ProjectModuleTypeServiceImpl();
    Gson gson=new Gson();
    TomcatAndHeartbeatServiceConfigService tomcatAndHeartbeatServiceConfigService=new TomcatAndHeartbeatServiceConfigServiceImpl();

    protected void ajaxCreateService(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException{
        String id=request.getParameter("id");
        String serverId=request.getParameter("server");
        String name=request.getParameter("name");
        String version=request.getParameter("version");
        //String ports=request.getParameter("ports");
//        List<String> ports=Arrays.asList(request.getParameter("ports").split(","));
        String customer=request.getParameter("customer");
        String programmer=request.getParameter("programmer");
        Date endTime=WebUtils.parseStringToDate(request.getParameter("endTime"));

        if(id==null){//新增
            //System.out.println(Thread.currentThread().getStackTrace()[1].getFileName()+":"
            //        +Thread.currentThread().getStackTrace()[1].getLineNumber()+":"+"进入serviceupdate,本次为新增");
            Service service=new Service(WebUtils.id(),serverId,name,version,"",customer,programmer,endTime);
            serviceService.addService(service);
        }else {//更新
            //System.out.println(Thread.currentThread().getStackTrace()[1].getFileName()+":"
            //        +Thread.currentThread().getStackTrace()[1].getLineNumber()+":"+"进入serviceupdate,id:"+id);
            Service service=serviceService.queryServiceById(id);
            if(service==null){
                response.getWriter().write("没有这个service");
                return;
            }else{
                service.setName(name);
                service.setVersion(version);
                service.setCustomer(customer);
                service.setProgrammer(programmer);
                service.setEndTime(endTime);
                //Service service=new Service(id,serverId,name,version,null,customer,programmer,endTime);
                serviceService.updateService(service);
            }
        }
        //Service service=new Service(null,"济宁8","1.1.1.1","4c / 4G",500, WebUtils.parseStringToDate("20210327"));
        //serviceService.addService(service);
        response.getWriter().write("存入成功");
    }
    protected void ajaxDeleteService(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException{
        String id=request.getParameter("id");
        Service service=serviceService.queryServiceById(id);
        if(service==null){
            response.getWriter().write("删除service的id,错误");
        }else {
            WebUtils.deleteService(service);
            response.getWriter().write("删除成功");
        }
    }
    protected void ajaxConfigService(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException{
        //System.out.println(request.getMethod());
        if(request.getMethod().equals("GET")){
            String serviceId=request.getParameter("serviceId");
            //System.out.println(serviceId);
            //response.getWriter().write("get");
            Service service=serviceService.queryServiceById(serviceId);
            //System.out.println(service);
            if(service!=null){
                //System.out.println("get 方式 获取到的 service是:"+service.toString());
                request.setAttribute("service",service);
                Server server =serverService.queryServerById(service.getServerId());
                request.setAttribute("server",server);
                if(service.getName().contains("nginx")){
                    List<NginxServiceConfigProxy> nginxServiceConfigProxies= nginxServiceConfigProxyService.queryNginxServiceConfigProxiesByServiceId(service.getId());
                    request.setAttribute("nginxServiceConfigProxies",nginxServiceConfigProxies);
                    List<NginxServiceConfigPhp> nginxServiceConfigPhps=nginxServiceConfigPhpService.queryNginxServiceConfigPhpsByServiceId(service.getId());
                    request.setAttribute("nginxServiceConfigPhps",nginxServiceConfigPhps);
                    List<NginxServiceConfigTcpProxy> nginxServiceConfigTcpProxies=nginxServiceConfigTcpProxyService.queryNginxServiceConfigTcpProxiesByServiceId(service.getId());
                    request.setAttribute("nginxServiceConfigTcpProxies",nginxServiceConfigTcpProxies);
                    //System.out.println(nginxServiceConfigProxies);
                    //LinkedHashSet<Project> projects=projectService.queryProjectByModuleType("php");

                    //查出module类型，供前端选择
                    List<ProjectModuleType> projectModuleTypes=projectModuleTypeService.queryProjectModuleTypes();
                    //把含有php模块的project给出来
                    LinkedHashSet<Project> projectsPhp=projectService.queryProjectsByModuleTypeName("php");
                    //System.out.println(projectsPhp);
                    //把php project的第一个的php的模块查出来
                    List<ProjectModule> projectModulesPhp=projectModuleService.queryProjectModulesByProjectIdAndProjectModuleTypeName(projectsPhp.iterator().next().getId(),"php");
                    //System.out.println(projectModulesPhp);
                    //收集第一个模块的全部节点
                    List<NginxServiceConfigPhp> nginxServiceConfigPhpsForProxy=nginxServiceConfigPhpService.queryNginxServiceConfigPhpsByProjectModuleId(projectModulesPhp.iterator().next().getId());

                    request.setAttribute("projectModuleTypes",projectModuleTypes);
                    request.setAttribute("nginxServiceConfigPhpsForProxy",nginxServiceConfigPhpsForProxy);
                    request.setAttribute("projectsPhp",projectsPhp);
                    request.setAttribute("projectModulesPhp",projectModulesPhp);
                    request.setAttribute("projectModuleService",projectModuleService);
                    request.setAttribute("projectService",projectService);
                    request.setAttribute("serverService",serverService);
                    request.setAttribute("serviceService",serviceService);
                    request.setAttribute("nginxServiceConfigPhpService",nginxServiceConfigPhpService);
                    request.setAttribute("nginxServiceConfigProxyService",nginxServiceConfigProxyService);
                    request.setAttribute("nginxServiceConfigTcpProxyService",nginxServiceConfigTcpProxyService);
                    request.setAttribute("tomcatServiceConfigService",tomcatServiceConfigService);
                    request.setAttribute("tomcatAndHeartbeatServiceConfigService",tomcatAndHeartbeatServiceConfigService);
                    request.setAttribute("projectModuleTypeService",projectModuleTypeService);

                    request.getRequestDispatcher("/pages/client/serviceConfigNginx.jsp").forward(request,response);
                }
                if(service.getName().contains("redis")){
                    RedisServiceConfig redisServiceConfig=redisServiceConfigService.queryRedisServiceConfigByServiceId(serviceId);
                    request.setAttribute("redisServiceConfig",redisServiceConfig);
                    request.getRequestDispatcher("/pages/client/serviceConfigRedis.jsp").forward(request,response);
                }
                if(service.getName().contains("mysql")){
                    MysqlServiceConfig mysqlServiceConfig= mysqlServiceConfigService.queryMysqlServiceConfigByServiceId(serviceId);
                    request.setAttribute("mysqlServiceConfig",mysqlServiceConfig);
                    request.getRequestDispatcher("/pages/client/serviceConfigMysql.jsp").forward(request,response);
                }
                if(service.getName().contains("mongo")){
                    MongoServiceConfig mongoServiceConfig=mongoServiceConfigService.queryMongoServiceConfigByServiceId(serviceId);
                    request.setAttribute("mongoServiceConfig",mongoServiceConfig);
                    request.getRequestDispatcher("/pages/client/serviceConfigMongo.jsp").forward(request,response);
                }
                if(service.getName().contains("docker")){
                    DockerServiceConfig dockerServiceConfig=dockerServiceConfigService.queryDockerServiceConfigByServiceId(serviceId);
                    request.setAttribute("dockerServiceConfig",dockerServiceConfig);
                    request.getRequestDispatcher("/pages/client/serviceConfigDocker.jsp").forward(request,response);
                }
                if(service.getName().contains("tomcat")){
                    TomcatServiceConfig tomcatServiceConfig=tomcatServiceConfigService.queryTomcatServiceConfigByServiceId(serviceId);
                    TomcatAndHeartbeatServiceConfig tomcatAndHeartbeatServiceConfig=tomcatAndHeartbeatServiceConfigService.queryTomcatAndHeartbeatServiceConfigByServiceId(serviceId);
                    request.setAttribute("tomcatServiceConfig",tomcatServiceConfig);
                    request.setAttribute("tomcatAndHeartbeatServiceConfig",tomcatAndHeartbeatServiceConfig);

                    if(tomcatServiceConfig==null && tomcatAndHeartbeatServiceConfig==null){//这是要新加啊
                        //说明还没有这个tomcat的配置
                        //查出tomcat的project,tomcatAndHeartbeat的project,以及tomcat的第一个project的module,tomcatAndHeartbeat的第一个project的module
                        ProjectModuleType projectModuleTypeTomcat=projectModuleTypeService.queryProjectModuleTypeByName("tomcat");
                        List<ProjectModule> projectModulesTomcatAll=projectModuleService.queryProjectModulesByProjectModuleTypeId(projectModuleTypeTomcat.getId());
                        Set<Project> projectsTomcat=new LinkedHashSet<>();
                        for (ProjectModule projectModule :projectModulesTomcatAll){
                            Project project=projectService.queryProjectByProjectId(projectModule.getProjectId());
                            projectsTomcat.add(project);
                        }
                        request.setAttribute("projectsTomcat",projectsTomcat);
                        List<ProjectModule> projectModulesTomcat=projectModuleService.queryProjectModulesByProjectIdAndProjectModuleTypeId(projectsTomcat.iterator().next().getId(),projectModuleTypeTomcat.getId());
                        request.setAttribute("projectModulesTomcat",projectModulesTomcat);

                        ProjectModuleType projectModuleTypeTomcatAndHeartbeat=projectModuleTypeService.queryProjectModuleTypeByName("tomcatAndHeartbeat");
                        List<ProjectModule> projectModulesTomcatAndHeartbeatAll=projectModuleService.queryProjectModulesByProjectModuleTypeId(projectModuleTypeTomcatAndHeartbeat.getId());
                        Set<Project> projectsTomcatAndHeartbeat=new LinkedHashSet<>();
                        for (ProjectModule projectModule :projectModulesTomcatAndHeartbeatAll){
                            Project project=projectService.queryProjectByProjectId(projectModule.getProjectId());
                            projectsTomcatAndHeartbeat.add(project);
                        }
                        request.setAttribute("projectsTomcatAndHeartbeat",projectsTomcatAndHeartbeat);
                        List<ProjectModule> projectModulesTomcatAndHeartbeat=projectModuleService.queryProjectModulesByProjectIdAndProjectModuleTypeId(projectsTomcatAndHeartbeat.iterator().next().getId(),projectModuleTypeTomcatAndHeartbeat.getId());
                        request.setAttribute("projectModulesTomcatAndHeartbeat",projectModulesTomcatAndHeartbeat);

                        request.getRequestDispatcher("/pages/client/serviceConfigTomcat.jsp").forward(request,response);
                    }else {
                        request.setAttribute("tomcatServiceConfig",tomcatServiceConfig);
                        request.setAttribute("tomcatAndHeartbeatServiceConfig",tomcatAndHeartbeatServiceConfig);
                        //System.out.println(tomcatAndHeartbeatServiceConfig);

//                        String projectModuleTypeId=projectModuleService.queryProjectModuleById(tomcatServiceConfig.getModuleId()).getProjectModuleTypeId();
//
//                        List<ProjectModule> projectModulesAllTomcat=projectModuleService.queryProjectModulesByProjectModuleTypeId(projectModuleTypeId);
//                        Set<Project> projects=new LinkedHashSet<>();
//                        for (ProjectModule projectModule :projectModulesAllTomcat){
//                            Project project=projectService.queryProjectByProjectId(projectModule.getProjectId());
//                            projects.add(project);
//                        }
//                        //获得项目列表,不重复的 用 LinkedHashSet
//                        //System.out.println(projects);
//                        request.setAttribute("projects",projects);
//
//                        List<ProjectModule> projectModules=projectModuleService.queryProjectModulesByProjectIdAndProjectModuleTypeId(tomcatServiceConfig.getProjectId(),projectModuleTypeId);
//                        request.setAttribute("projectModules",projectModules);

                        ProjectModuleType projectModuleTypeTomcat=projectModuleTypeService.queryProjectModuleTypeByName("tomcat");
                        List<ProjectModule> projectModulesTomcatAll=projectModuleService.queryProjectModulesByProjectModuleTypeId(projectModuleTypeTomcat.getId());
                        Set<Project> projectsTomcat=new LinkedHashSet<>();
                        for (ProjectModule projectModule :projectModulesTomcatAll){
                            Project project=projectService.queryProjectByProjectId(projectModule.getProjectId());
                            projectsTomcat.add(project);
                        }
                        request.setAttribute("projectsTomcat",projectsTomcat);
                        List<ProjectModule> projectModulesTomcat=projectModuleService.queryProjectModulesByProjectIdAndProjectModuleTypeId(projectsTomcat.iterator().next().getId(),projectModuleTypeTomcat.getId());
                        request.setAttribute("projectModulesTomcat",projectModulesTomcat);

                        ProjectModuleType projectModuleTypeTomcatAndHeartbeat=projectModuleTypeService.queryProjectModuleTypeByName("tomcatAndHeartbeat");
                        List<ProjectModule> projectModulesTomcatAndHeartbeatAll=projectModuleService.queryProjectModulesByProjectModuleTypeId(projectModuleTypeTomcatAndHeartbeat.getId());
                        Set<Project> projectsTomcatAndHeartbeat=new LinkedHashSet<>();
                        for (ProjectModule projectModule :projectModulesTomcatAndHeartbeatAll){
                            Project project=projectService.queryProjectByProjectId(projectModule.getProjectId());
                            projectsTomcatAndHeartbeat.add(project);
                        }
                        request.setAttribute("projectsTomcatAndHeartbeat",projectsTomcatAndHeartbeat);
                        List<ProjectModule> projectModulesTomcatAndHeartbeat=projectModuleService.queryProjectModulesByProjectIdAndProjectModuleTypeId(projectsTomcatAndHeartbeat.iterator().next().getId(),projectModuleTypeTomcatAndHeartbeat.getId());
                        request.setAttribute("projectModulesTomcatAndHeartbeat",projectModulesTomcatAndHeartbeat);

                        request.getRequestDispatcher("/pages/client/serviceConfigTomcat.jsp").forward(request,response);
                    }

                }
            }else{
                response.getWriter().write("serviceId: "+serviceId+" not found");
            }
        }else{//应对post methed
            String configType=request.getParameter("configType");
            //System.out.println("这是 ServiceServlet.ajaxConfigService 的提示,configType 是: "+configType);
            if(configType.equals("redis")){
                String serviceId=request.getParameter("serviceId");
                String parameters=request.getParameter("parameters");
                System.out.println("这是 ServiceServlet.ajaxConfigService 的提示,parameters 是: "+request.getParameter("parameters"));

                RedisServiceConfig redisServiceConfig=redisServiceConfigService.queryRedisServiceConfigByServiceId(serviceId);

                if (redisServiceConfig!=null){
                    if(request.getParameter("delete")!=null){
                        if(request.getParameter("delete").equals("true")){//那么这就是要删除
                            WebUtils.deleteRedisServiceConfig(redisServiceConfig);//这个方法中包含有递归删除port占用的方法
                            response.getWriter().write("删除成功");
                            return;
                        }
                    }
                }//删除的逻辑

                String port=request.getParameter("port");
                String password=request.getParameter("password");
                String maxMemory=request.getParameter("maxMemory");
                String masterNode=request.getParameter("masterNode");

                if(port==null|password==null|maxMemory==null|masterNode==null|Objects.equals(port, "") | Objects.equals(password, "") |Objects.equals(maxMemory, "")|Objects.equals(masterNode, "")){
                    response.getWriter().write("传参有问题");
                    return;
                }//传参判断正常与否

                if(redisServiceConfig==null){//找不到这个 redisServiceConfig，那么就是add
                    Service service=serviceService.queryServiceById(serviceId);
                    if(service==null){
                        response.getWriter().write("新增加 redisConfig 时,serviceId有问题:"+serviceId);
                    }else {
                        //新端口允许使用的逻辑
                        String serverPorts=serverService.queryServerById(service.getServerId()).getPorts();
                        if(serverPorts.contains(port)){//这是新增,所以不存在无损更新的情况;所以,要放入的端口,一定不能出现在serverPorts中
                            response.getWriter().write("新增加 redisConfig 时,端口冲突");
                            return;
                        }else{
                            //System.out.println("服务器上没有使用这个端口号,可以使用");
                        }
                        //新端口允许使用的逻辑-完
                        //老端口要不要释放的逻辑
                        //add没有释放老端口这么一说
                        //老端口要不要释放的逻辑-完
                    }//整台服务器不能有与之冲突的端口号记录存在
                    Server server=serverService.queryServerById(service.getServerId());

                    RedisServiceConfig redisServiceConfigAdd=new RedisServiceConfig();
                    redisServiceConfigAdd.setRedisServiceConfigId(WebUtils.id());
                    redisServiceConfigAdd.setServerId(server.getId());
                    redisServiceConfigAdd.setServiceId(service.getId());
                    redisServiceConfigAdd.setPort(port);
                    redisServiceConfigAdd.setPassword(password);
                    redisServiceConfigAdd.setMaxMemory(maxMemory);
                    redisServiceConfigAdd.setMasterNode(masterNode);

                    redisServiceConfigService.addRedisServiceConfig(redisServiceConfigAdd);

                    service.setPorts(WebUtils.addPortsItem(service.getPorts(),port));
                    serviceService.updateService(service);

                    response.getWriter().write("存入成功");
                }else {//找得到这个 redisServiceConfig,那就是update
                    //对于单一配置文件的服务来说,serviceId会出现在add和update中,因为即便是update,那所指的配置项也只有一条
                    Service service=serviceService.queryServiceById(serviceId);

                    //新端口允许使用的逻辑
                    String serverPorts=serverService.queryServerById(service.getServerId()).getPorts();
                    if(serverPorts.contains(port)){//万一是无损更新呢,只需对比一下,老端口和新端口是否一样.一样就过,不一样就处理
                        if(redisServiceConfig.getPort().equals(port)){
                            //System.out.println("更新前和更新后,端口号一样,不冲突,可以继续更新");
                        }else{
                            //System.out.println("更新前和更新后,端口号不一样,说明:新端口号是别的服务在占用,但又不是本服务在用.因此,产生冲突.所以必须停止继续更新");
                            response.getWriter().write("端口冲突");
                            return;
                        }
                    }else{
                        //System.out.println("服务器上没有使用这个端口号,可以使用");
                    }//整台服务器不能有与之冲突的端口号记录存在
                    //新端口允许使用的逻辑-完
                    //老端口要不要释放的逻辑
                    if(redisServiceConfig.getPort().equals(port)){
                        //System.out.println("新老端口一样,不必释放老端口");
                    }else {
                        //System.out.println("这是redis,不用了,一定不会有复用的情况,所以应该释放");
                        String newServicePorts=WebUtils.reducePortItem(service.getPorts(),redisServiceConfig.getPort());
                        service.setPorts(newServicePorts);
                        serviceService.updateService(service);
                    }
                    //老端口要不要释放的逻辑-完
                    redisServiceConfig.setPort(port);
                    redisServiceConfig.setPassword(password);
                    redisServiceConfig.setMaxMemory(maxMemory);
                    redisServiceConfig.setMasterNode(masterNode);
                    redisServiceConfigService.updateRedisServiceConfig(redisServiceConfig);

                    service.setPorts(WebUtils.addPortsItem(service.getPorts(),port));
                    serviceService.updateService(service);

                    response.getWriter().write("存入成功");
                }//找得到-更新,找不到-添加
            }
            if(configType.equals("mysql")){
                String serviceId=request.getParameter("serviceId");
                MysqlServiceConfig mysqlServiceConfig=mysqlServiceConfigService.queryMysqlServiceConfigByServiceId(serviceId);

                if (mysqlServiceConfig!=null){
                    if(request.getParameter("delete")!=null){
                        if(request.getParameter("delete").equals("true")){//那么这就是要删除
                            WebUtils.deleteMysqlServiceConfig(mysqlServiceConfig);//这个方法中包含有递归删除port占用的方法
                            response.getWriter().write("删除成功");
                            return;
                        }
                    }
                }//删除的逻辑

                String port=request.getParameter("port");
                String password=request.getParameter("password");
                String innodb_buffer_pool_size=request.getParameter("innodb_buffer_pool_size");
                String max_connections=request.getParameter("max_connections");
                String masterNode=request.getParameter("masterNode");

                if(password==null|password==null|innodb_buffer_pool_size==null|max_connections==null|masterNode==null|Objects.equals(port, "") | Objects.equals(password, "") |Objects.equals(innodb_buffer_pool_size, "")|Objects.equals(max_connections, "")|Objects.equals(masterNode, "")){
                    response.getWriter().write("传参有问题");
                    return;
                }//传参判断正常与否

                if(mysqlServiceConfig==null){//找不到这个 mysqlServiceConfig，那么就是add
                    Service service=serviceService.queryServiceById(serviceId);
                    if(service==null){
                        response.getWriter().write("新增加 mysqlConfig 时,serviceId有问题:"+serviceId);
                    }else {
                        //新端口允许使用的逻辑
                        String serverPorts=serverService.queryServerById(service.getServerId()).getPorts();
                        if(serverPorts.contains(port)){//这是新增,所以不存在无损更新的情况;所以,要放入的端口,一定不能出现在serverPorts中
                            response.getWriter().write("新增加 mysqlConfig 时,端口冲突");
                            return;
                        }else{
                            //System.out.println("服务器上没有使用这个端口号,可以使用");
                        }
                        //新端口允许使用的逻辑-完
                        //老端口要不要释放的逻辑
                        //add没有释放老端口这么一说
                        //老端口要不要释放的逻辑-完
                    }//整台服务器不能有与之冲突的端口号记录存在
                    Server server=serverService.queryServerById(service.getServerId());

                    MysqlServiceConfig mysqlServiceConfigAdd=new MysqlServiceConfig();
                    mysqlServiceConfigAdd.setId(WebUtils.id());
                    mysqlServiceConfigAdd.setServerId(server.getId());
                    mysqlServiceConfigAdd.setServiceId(service.getId());
                    mysqlServiceConfigAdd.setPort(port);
                    mysqlServiceConfigAdd.setPassword(password);
                    mysqlServiceConfigAdd.setInnodb_buffer_pool_size(innodb_buffer_pool_size);
                    mysqlServiceConfigAdd.setMax_connections(max_connections);
                    mysqlServiceConfigAdd.setMasterNode(masterNode);

                    mysqlServiceConfigService.addMysqlServiceConfig(mysqlServiceConfigAdd);

                    service.setPorts(WebUtils.addPortsItem(service.getPorts(),port));
                    serviceService.updateService(service);

                    response.getWriter().write("存入成功");
                }else {//找得到这个 mysqlServiceConfig,那就是update
                    //对于单一配置文件的服务来说,serviceId会出现在add和update中,因为即便是update,那所指的配置项也只有一条
                    Service service=serviceService.queryServiceById(serviceId);

                    //新端口允许使用的逻辑
                    String serverPorts=serverService.queryServerById(service.getServerId()).getPorts();
                    if(serverPorts.contains(port)){//万一是无损更新呢,只需对比一下,老端口和新端口是否一样.一样就过,不一样就处理
                        if(mysqlServiceConfig.getPort().equals(port)){
                            //System.out.println("更新前和更新后,端口号一样,不冲突,可以继续更新");
                        }else{
                            //System.out.println("更新前和更新后,端口号不一样,说明:新端口号是别的服务在占用,但又不是本服务在用.因此,产生冲突.所以必须停止继续更新");
                            response.getWriter().write("端口冲突");
                            return;
                        }
                    }else{
                        //System.out.println("服务器上没有使用这个端口号,可以使用");
                    }//整台服务器不能有与之冲突的端口号记录存在
                    //新端口允许使用的逻辑-完
                    //老端口要不要释放的逻辑
                    if(mysqlServiceConfig.getPort().equals(port)){
                        //System.out.println("新老端口一样,不必释放老端口");
                    }else {
                        //System.out.println("这是mysql,不用了,一定不会有复用的情况,所以应该释放");
                        String newServicePorts=WebUtils.reducePortItem(service.getPorts(),mysqlServiceConfig.getPort());
                        service.setPorts(newServicePorts);
                        serviceService.updateService(service);
                    }
                    //老端口要不要释放的逻辑-完
                    mysqlServiceConfig.setPort(port);
                    mysqlServiceConfig.setPassword(password);
                    mysqlServiceConfig.setInnodb_buffer_pool_size(innodb_buffer_pool_size);
                    mysqlServiceConfig.setMax_connections(max_connections);
                    mysqlServiceConfig.setMasterNode(masterNode);
                    mysqlServiceConfigService.updateMysqlServiceConfig(mysqlServiceConfig);

                    service.setPorts(WebUtils.addPortsItem(service.getPorts(),port));
                    serviceService.updateService(service);

                    response.getWriter().write("存入成功");
                }//找得到-更新,找不到-添加
            }
            if(configType.equals("mongo")){
                String serviceId=request.getParameter("serviceId");
                MongoServiceConfig mongoServiceConfig=mongoServiceConfigService.queryMongoServiceConfigByServiceId(serviceId);

                if (mongoServiceConfig!=null){
                    if(request.getParameter("delete")!=null){
                        if(request.getParameter("delete").equals("true")){//那么这就是要删除
                            WebUtils.deleteMongoServiceConfig(mongoServiceConfig);//这个方法中包含有递归删除port占用的方法
                            response.getWriter().write("删除成功");
                            return;
                        }
                    }
                }//删除的逻辑

                String port=request.getParameter("port");
                String password=request.getParameter("password");
                String maxConns=request.getParameter("maxConns");
                String masterNode=request.getParameter("masterNode");

                if(Objects.equals(port, "") | Objects.equals(password, "") |Objects.equals(maxConns, "")|Objects.equals(masterNode, "")|port==null|password==null|maxConns==null|masterNode==null){
                    response.getWriter().write("传参有问题");
                    return;
                }//传参判断正常与否

                if(mongoServiceConfig==null){//找不到这个 mongoServiceConfig，那么就是add
                    Service service=serviceService.queryServiceById(serviceId);
                    if(service==null){
                        response.getWriter().write("新增加 mongoConfig 时,serviceId有问题:"+serviceId);
                    }else {
                        //新端口允许使用的逻辑
                        String serverPorts=serverService.queryServerById(service.getServerId()).getPorts();
                        if(serverPorts.contains(port)){//这是新增,所以不存在无损更新的情况;所以,要放入的端口,一定不能出现在serverPorts中
                            response.getWriter().write("新增加 mongoConfig 时,端口冲突");
                            return;
                        }else{
                            //System.out.println("服务器上没有使用这个端口号,可以使用");
                        }
                        //新端口允许使用的逻辑-完
                        //老端口要不要释放的逻辑
                        //add没有释放老端口这么一说
                        //老端口要不要释放的逻辑-完
                    }//整台服务器不能有与之冲突的端口号记录存在
                    Server server=serverService.queryServerById(service.getServerId());

                    MongoServiceConfig mongoServiceConfigAdd=new MongoServiceConfig();
                    mongoServiceConfigAdd.setId(WebUtils.id());
                    mongoServiceConfigAdd.setServerId(server.getId());
                    mongoServiceConfigAdd.setServiceId(service.getId());
                    mongoServiceConfigAdd.setPort(port);
                    mongoServiceConfigAdd.setPassword(password);
                    mongoServiceConfigAdd.setMaxConns(maxConns);
                    mongoServiceConfigAdd.setMasterNode(masterNode);

                    mongoServiceConfigService.addMongoServiceConfig(mongoServiceConfigAdd);

                    service.setPorts(WebUtils.addPortsItem(service.getPorts(),port));
                    serviceService.updateService(service);

                    response.getWriter().write("存入成功");
                }else {//找得到这个 mongoServiceConfig,那就是update
                    //对于单一配置文件的服务来说,serviceId会出现在add和update中,因为即便是update,那所指的配置项也只有一条
                    Service service=serviceService.queryServiceById(serviceId);

                    //新端口允许使用的逻辑
                    String serverPorts=serverService.queryServerById(service.getServerId()).getPorts();
                    if(serverPorts.contains(port)){//万一是无损更新呢,只需对比一下,老端口和新端口是否一样.一样就过,不一样就处理
                        if(mongoServiceConfig.getPort().equals(port)){
                            //System.out.println("更新前和更新后,端口号一样,不冲突,可以继续更新");
                        }else{
                            //System.out.println("更新前和更新后,端口号不一样,说明:新端口号是别的服务在占用,但又不是本服务在用.因此,产生冲突.所以必须停止继续更新");
                            response.getWriter().write("端口冲突");
                            return;
                        }
                    }else{
                        //System.out.println("服务器上没有使用这个端口号,可以使用");
                    }//整台服务器不能有与之冲突的端口号记录存在
                    //新端口允许使用的逻辑-完
                    //老端口要不要释放的逻辑
                    if(mongoServiceConfig.getPort().equals(port)){
                        //System.out.println("新老端口一样,不必释放老端口");
                    }else {
                        //System.out.println("这是mongo,不用了,一定不会有复用的情况,所以应该释放");
                        String newServicePorts=WebUtils.reducePortItem(service.getPorts(),mongoServiceConfig.getPort());
                        service.setPorts(newServicePorts);
                        serviceService.updateService(service);
                    }
                    //老端口要不要释放的逻辑-完
                    mongoServiceConfig.setPort(port);
                    mongoServiceConfig.setPassword(password);
                    mongoServiceConfig.setMaxConns(maxConns);
                    mongoServiceConfig.setMasterNode(masterNode);
                    mongoServiceConfigService.updateMongoServiceConfig(mongoServiceConfig);

                    service.setPorts(WebUtils.addPortsItem(service.getPorts(),port));
                    serviceService.updateService(service);

                    response.getWriter().write("存入成功");
                }//找得到-更新,找不到-添加
            }
            if(configType.equals("docker")){
                String serviceId=request.getParameter("serviceId");
                DockerServiceConfig dockerServiceConfig=dockerServiceConfigService.queryDockerServiceConfigByServiceId(serviceId);

                if (dockerServiceConfig!=null){
                    if(request.getParameter("delete")!=null){
                        if(request.getParameter("delete").equals("true")){//那么这就是要删除
                            WebUtils.deleteDockerServiceConfig(dockerServiceConfig);//这个方法中包含有递归删除port占用的方法
                            response.getWriter().write("删除成功");
                            return;
                        }
                    }
                }//删除的逻辑

                String graph=request.getParameter("graph");
                String bip=request.getParameter("bip");

                if(Objects.equals(graph, "") | Objects.equals(bip, "")|graph==null|bip==null ){
                    response.getWriter().write("传参有问题");
                    return;
                }//传参判断正常与否

                if(dockerServiceConfig==null){//找不到这个 dockerServiceConfig，那么就是add
                    Service service=serviceService.queryServiceById(serviceId);
                    //docker配置文件中不包含端口号,不需要做端口号的重复性判断
                    Server server=serverService.queryServerById(service.getServerId());

                    DockerServiceConfig dockerServiceConfigAdd=new DockerServiceConfig();
                    dockerServiceConfigAdd.setId(WebUtils.id());
                    dockerServiceConfigAdd.setServerId(server.getId());
                    dockerServiceConfigAdd.setServiceId(service.getId());
                    dockerServiceConfigAdd.setGraph(graph);
                    dockerServiceConfigAdd.setBip(bip);

                    dockerServiceConfigService.addDockerServiceConfig(dockerServiceConfigAdd);
                    response.getWriter().write("存入成功");
                }else {//找得到这个 dockerServiceConfig,那就是update
                    //对于单一配置文件的服务来说,serviceId会出现在add和update中,因为即便是update,那所指的配置项也只有一条
                    Service service=serviceService.queryServiceById(serviceId);
                    //docker配置文件中不包含端口号,不需要做端口号的重复性判断,以及老端口释放工作
                    dockerServiceConfig.setGraph(graph);
                    dockerServiceConfig.setBip(bip);
                    dockerServiceConfigService.updateDockerServiceConfig(dockerServiceConfig);

                    response.getWriter().write("存入成功");
                }//找得到-更新,找不到-添加
            }
            if(configType.equals("tomcat")){
                String serviceId=request.getParameter("serviceId");
                TomcatAndHeartbeatServiceConfig tomcatAndHeartbeatServiceConfig=tomcatAndHeartbeatServiceConfigService.queryTomcatAndHeartbeatServiceConfigByServiceId(serviceId);
                if(tomcatAndHeartbeatServiceConfig==null){
                    //没有这么一个 heartbeat 配置，所以可以过。可以考虑给tomcat加外挂，这样tomcat是tomcat，有没有外挂，在执行附加的行为。这样tomcat服务就不用不停的加类型了。而且这个外挂还能给其他种类的服务用，比如redis，django
                }else {
                    WebUtils.deleteTomcatAndHeartbeatServiceConfig(tomcatAndHeartbeatServiceConfig);
                }//这是防止用户把tomcat服务切换成tomcatAndHeartbeat服务，先删老配置，再加新配置
                TomcatServiceConfig tomcatServiceConfig=tomcatServiceConfigService.queryTomcatServiceConfigByServiceId(serviceId);

                if (tomcatServiceConfig!=null){
                    if(request.getParameter("delete")!=null){
                        if(request.getParameter("delete").equals("true")){//那么这就是要删除
                            WebUtils.deleteTomcatServiceConfig(tomcatServiceConfig);//这个方法中包含有递归删除port占用的方法
                            response.getWriter().write("删除成功");
                            return;
                        }
                    }
                }//删除的逻辑

                String tomcatHome=request.getParameter("tomcatHome");
                String javaHome=request.getParameter("javaHome");
                String maxMemory=request.getParameter("maxMemory");
                String httpPort=request.getParameter("httpPort");
                String shutdownPort=request.getParameter("shutdownPort");
                String projectId=request.getParameter("project");
                String moduleId=request.getParameter("module");
                String use=request.getParameter("use");
                String heartbeat=request.getParameter("heartbeat");
                //System.out.println(projectId+","+moduleId);
                if (httpPort.equals(shutdownPort)){//避免同一个配置文件中2个端口配成一样的了的情况
                    response.getWriter().write("httpPort:"+httpPort+",不能等于 shutdownPort:"+shutdownPort);
                    return;
                }//避免同一个配置文件中2个端口配成一样的了的情况


                if(Objects.equals(tomcatHome, "") | Objects.equals(javaHome, "")|Objects.equals(maxMemory, "") |
                        Objects.equals(httpPort, "")|Objects.equals(shutdownPort, "") |
                        Objects.equals(projectId, "")|Objects.equals(moduleId, "") |
                        tomcatHome==null|javaHome==null|maxMemory==null |httpPort==null |shutdownPort==null|projectId==null|moduleId==null){
                    response.getWriter().write("传参有问题");return;
                }//传参判断正常与否

                Service service=serviceService.queryServiceById(serviceId);
                if(service==null){
                    response.getWriter().write("此serviceId not found:"+serviceId);return;
                }else {
                    //否则,继续
                }

                if(tomcatServiceConfig==null){//找不到这个 tomcatServiceConfig，那么就是add

                    if(service==null){
                        response.getWriter().write("新增加 tomcatConfig 时,serviceId有问题:"+serviceId);return;
                    }else {
                        //新端口允许使用的逻辑
                        String serverPorts=serverService.queryServerById(service.getServerId()).getPorts();
                        if(serverPorts.contains(httpPort)||serverPorts.contains(shutdownPort)){//这是新增,所以不存在无损更新的情况;所以,要放入的端口,一定不能出现在serverPorts中
                            response.getWriter().write("新增加 tomcatConfig 时,端口冲突");return;
                        }else{
                            //System.out.println("服务器上没有使用这个端口号,可以使用");
                        }
                        //新端口允许使用的逻辑-完
                        //老端口要不要释放的逻辑
                        //add没有释放老端口这么一说
                        //老端口要不要释放的逻辑-完
                    }//整台服务器不能有与之冲突的端口号记录存在

                    //tomcat配置文件中不包含端口号,不需要做端口号的重复性判断
                    Server server=serverService.queryServerById(service.getServerId());

                    TomcatServiceConfig tomcatServiceConfigAdd=new TomcatServiceConfig();
                    tomcatServiceConfigAdd.setId(WebUtils.id());
                    tomcatServiceConfigAdd.setServerId(server.getId());
                    tomcatServiceConfigAdd.setServiceId(service.getId());
                    tomcatServiceConfigAdd.setTomcatHome(tomcatHome);
                    tomcatServiceConfigAdd.setJavaHome(javaHome);
                    tomcatServiceConfigAdd.setMaxMemory(maxMemory);
                    tomcatServiceConfigAdd.setHttpPort(httpPort);
                    tomcatServiceConfigAdd.setShutdownPort(shutdownPort);
                    tomcatServiceConfigAdd.setProjectId(projectId);
                    tomcatServiceConfigAdd.setModuleId(moduleId);
                    tomcatServiceConfigAdd.setUse(use);
                    tomcatServiceConfigAdd.setHeartbeat(heartbeat);

                    tomcatServiceConfigService.addTomcatServiceConfig(tomcatServiceConfigAdd);
                    String ports=WebUtils.addPortsItem(service.getPorts(),httpPort);
                    ports=WebUtils.addPortsItem(ports,shutdownPort);
                    service.setPorts(ports);
                    serviceService.updateService(service);

                    response.getWriter().write("存入成功");
                }else {//找得到这个 tomcatServiceConfig,那就是update
                    //对于单一配置文件的服务来说,serviceId会出现在add和update中,因为即便是update,那所指的配置项也只有一条

                    //新端口允许使用的逻辑
                    String serverPorts=serverService.queryServerById(service.getServerId()).getPorts();

                    if(serverPorts.contains(httpPort)||serverPorts.contains(shutdownPort)){//万一是无损更新呢,只需对比一下,老端口和新端口是否一样.一样就过,不一样就处理
                        if(tomcatServiceConfig.getHttpPort().equals(httpPort) && tomcatServiceConfig.getShutdownPort().equals(shutdownPort)){
                            //System.out.println("更新前和更新后,端口号一样,不冲突,可以继续更新");
                        }else{
                            //System.out.println("更新前和更新后,端口号不一样,说明:新端口号是别的服务在占用,但又不是本服务在用.因此,产生冲突.所以必须停止继续更新");
                            //那万一是服务内部互换,则不必视作冲突
                            if(tomcatServiceConfig.getHttpPort().equals(shutdownPort) && tomcatServiceConfig.getShutdownPort().equals(httpPort)){
                                //这便是服务内部端口互换,不视为冲突,可以继续更新
                            }else{
                                //否则,就不是服务内部端口互换,那就有可能是某一个端口被包含在了server使用中,或是全部包含在了server使用列表中.则不能继续更新,需要去查验
                                //response.getWriter().write("端口冲突,请查验 httpPort:"+httpPort+" ,shutdownPort: "+shutdownPort+" 到底哪个端口号冲突了");
                                //return;
                                String result="";//对比完后,结果仍为空,则包含,而不算冲突
                               if(serverPorts.contains(httpPort)){
                                   // 被包含的是 httpPort
                                   if (tomcatServiceConfig.getHttpPort().equals(httpPort)){
                                       //httpPort被包含,但与此前一致,过
                                   }else {
                                       //否则,就是 包含,但不一致,则为冲突
                                       result=result+"httpPort已被使用:"+httpPort;
                                   }
                               }else {
                                   // 看来被包含的不是 httpPort,httpPort可以启用
                               }
                                if(serverPorts.contains(shutdownPort)){
                                    // 被包含的是 shutdownPort
                                    if (tomcatServiceConfig.getShutdownPort().equals(shutdownPort)){
                                        //shutdownPort 被包含,但与此前一致,过
                                    }else {
                                        //否则,就是 包含,但不一致,则为冲突
                                        if (result.equals("")){
                                            result="shutdownPort已被使用:"+shutdownPort;
                                        }else {
                                            result=result+",shutdownPort已被使用:"+shutdownPort;
                                        }
                                    }
                                }else {
                                    // 看来被包含的不是 shutdownPort,shutdownPort 可以启用
                                }
                                if(result.equals("")){
                                    //结果为空,因此为包含,但前后一致,过
                                }else {
                                    //结果不为空,则返回用户,呈现异常
                                    response.getWriter().write(result);
                                    return;
                                }
                                //无论如何,这是serverPort包含了要提交的端口号,目的是挑出产生冲突的那个端口.亦或,端口跟此前的一样,就不算冲突了
                            }
                        }
                    }else{
                        //System.out.println("服务器上没有使用这个端口号,可以使用");
                    }//整台服务器不能有与之冲突的端口号记录存在
                    //新端口允许使用的逻辑-完
                    //老端口要不要释放的逻辑
                    if(tomcatServiceConfig.getHttpPort().equals(httpPort) && tomcatServiceConfig.getShutdownPort().equals(shutdownPort)){
                        //System.out.println("新老端口一样,不必释放老端口");
                    }else {
                        if (tomcatServiceConfig.getHttpPort().equals(shutdownPort) && tomcatServiceConfig.getShutdownPort().equals(httpPort)){
                            //此为服务内部端口互换,所以不必清理端口占用情况
                        }else {
                            //System.out.println("端口占用前后不一致,又不是内部互换,则必然有一方或多方是新用,那么必然有一方或多方是弃用,因此要全部放弃,后而加上");
                            String newServicePorts=WebUtils.reducePortItem(service.getPorts(),tomcatServiceConfig.getHttpPort());
                            newServicePorts=WebUtils.reducePortItem(newServicePorts,tomcatServiceConfig.getShutdownPort()); //本服务有2个端口,故而需要连续减除2次
                            service.setPorts(newServicePorts);
                            serviceService.updateService(service);
                        }

                    }
                    //老端口要不要释放的逻辑-完
                    tomcatServiceConfig.setTomcatHome(tomcatHome);
                    tomcatServiceConfig.setJavaHome(javaHome);
                    tomcatServiceConfig.setMaxMemory(maxMemory);
                    tomcatServiceConfig.setHttpPort(httpPort);
                    tomcatServiceConfig.setShutdownPort(shutdownPort);
                    tomcatServiceConfig.setProjectId(projectId);
                    tomcatServiceConfig.setModuleId(moduleId);
                    tomcatServiceConfig.setUse(use);
                    tomcatServiceConfig.setHeartbeat(heartbeat);

                    tomcatServiceConfigService.updateTomcatServiceConfig(tomcatServiceConfig);
                    String ports=WebUtils.addPortsItem(service.getPorts(),httpPort);
                    ports=WebUtils.addPortsItem(ports,shutdownPort);
                    service.setPorts(ports);
                    serviceService.updateService(service);

                    response.getWriter().write("存入成功");
                }//找得到-更新,找不到-添加
            }
            if(configType.equals("tomcatAndHeartbeat")){
                String serviceId=request.getParameter("serviceId");
                TomcatServiceConfig tomcatServiceConfig=tomcatServiceConfigService.queryTomcatServiceConfigByServiceId(serviceId);
                if(tomcatServiceConfig==null){
                    //没有这么一个 tomcat 配置，所以可以过。可以考虑给tomcatAndHeartbeat加外挂，这样tomcatAndHeartbeat是tomcatAndHeartbeat，有没有外挂，在执行附加的行为。这样tomcatAndHeartbeat服务就不用不停的加类型了。而且这个外挂还能给其他种类的服务用，比如redis，django
                }else {
                    WebUtils.deleteTomcatServiceConfig(tomcatServiceConfig);
                }//这是防止用户把tomcat服务切换成tomcatAndHeartbeat服务，先删老配置，再加新配置
                TomcatAndHeartbeatServiceConfig tomcatAndHeartbeatServiceConfig=tomcatAndHeartbeatServiceConfigService.queryTomcatAndHeartbeatServiceConfigByServiceId(serviceId);

                if (tomcatAndHeartbeatServiceConfig!=null){
                    if(request.getParameter("delete")!=null){
                        if(request.getParameter("delete").equals("true")){//那么这就是要删除
                            WebUtils.deleteTomcatAndHeartbeatServiceConfig(tomcatAndHeartbeatServiceConfig);//这个方法中包含有递归删除port占用的方法
                            response.getWriter().write("删除成功");
                            return;
                        }
                    }
                }//删除的逻辑

                String tomcatHome=request.getParameter("tomcatHome");
                String javaHome=request.getParameter("javaHome");
                String maxMemory=request.getParameter("maxMemory");
                String httpPort=request.getParameter("httpPort");
                String shutdownPort=request.getParameter("shutdownPort");
                String projectId=request.getParameter("project");
                String moduleId=request.getParameter("module");
                String use=request.getParameter("use");
                String heartbeat=request.getParameter("heartbeat");
                //System.out.println(projectId+","+moduleId);
                if (httpPort.equals(shutdownPort)){//避免同一个配置文件中2个端口配成一样的了的情况
                    response.getWriter().write("httpPort:"+httpPort+",不能等于 shutdownPort:"+shutdownPort);
                    return;
                }//避免同一个配置文件中2个端口配成一样的了的情况


                if(Objects.equals(tomcatHome, "") | Objects.equals(javaHome, "")|Objects.equals(maxMemory, "") |
                        Objects.equals(httpPort, "")|Objects.equals(shutdownPort, "") |
                        Objects.equals(projectId, "")|Objects.equals(moduleId, "") |
                        tomcatHome==null|javaHome==null|maxMemory==null |httpPort==null |shutdownPort==null|projectId==null|moduleId==null){
                    response.getWriter().write("传参有问题");return;
                }//传参判断正常与否

                Service service=serviceService.queryServiceById(serviceId);
                if(service==null){
                    response.getWriter().write("此serviceId not found:"+serviceId);return;
                }else {
                    //否则,继续
                }

                if(tomcatAndHeartbeatServiceConfig==null){//找不到这个 tomcatAndHeartbeatServiceConfig，那么就是add

                    if(service==null){
                        response.getWriter().write("新增加 tomcatAndHeartbeatConfig 时,serviceId有问题:"+serviceId);return;
                    }else {
                        //新端口允许使用的逻辑
                        String serverPorts=serverService.queryServerById(service.getServerId()).getPorts();
                        if(serverPorts.contains(httpPort)||serverPorts.contains(shutdownPort)){//这是新增,所以不存在无损更新的情况;所以,要放入的端口,一定不能出现在serverPorts中
                            response.getWriter().write("新增加 tomcatAndHeartbeatConfig 时,端口冲突");return;
                        }else{
                            //System.out.println("服务器上没有使用这个端口号,可以使用");
                        }
                        //新端口允许使用的逻辑-完
                        //老端口要不要释放的逻辑
                        //add没有释放老端口这么一说
                        //老端口要不要释放的逻辑-完
                    }//整台服务器不能有与之冲突的端口号记录存在

                    //tomcatAndHeartbeat配置文件中不包含端口号,不需要做端口号的重复性判断
                    Server server=serverService.queryServerById(service.getServerId());

                    TomcatAndHeartbeatServiceConfig tomcatAndHeartbeatServiceConfigAdd=new TomcatAndHeartbeatServiceConfig();
                    tomcatAndHeartbeatServiceConfigAdd.setId(WebUtils.id());
                    tomcatAndHeartbeatServiceConfigAdd.setServerId(server.getId());
                    tomcatAndHeartbeatServiceConfigAdd.setServiceId(service.getId());
                    tomcatAndHeartbeatServiceConfigAdd.setTomcatHome(tomcatHome);
                    tomcatAndHeartbeatServiceConfigAdd.setJavaHome(javaHome);
                    tomcatAndHeartbeatServiceConfigAdd.setMaxMemory(maxMemory);
                    tomcatAndHeartbeatServiceConfigAdd.setHttpPort(httpPort);
                    tomcatAndHeartbeatServiceConfigAdd.setShutdownPort(shutdownPort);
                    tomcatAndHeartbeatServiceConfigAdd.setProjectId(projectId);
                    tomcatAndHeartbeatServiceConfigAdd.setModuleId(moduleId);
                    tomcatAndHeartbeatServiceConfigAdd.setUse(use);
                    tomcatAndHeartbeatServiceConfigAdd.setHeartbeat(heartbeat);

                    tomcatAndHeartbeatServiceConfigService.addTomcatAndHeartbeatServiceConfig(tomcatAndHeartbeatServiceConfigAdd);
                    String ports=WebUtils.addPortsItem(service.getPorts(),httpPort);
                    ports=WebUtils.addPortsItem(ports,shutdownPort);
                    service.setPorts(ports);
                    serviceService.updateService(service);

                    response.getWriter().write("存入成功");
                }else {//找得到这个 tomcatAndHeartbeatServiceConfig,那就是update
                    //对于单一配置文件的服务来说,serviceId会出现在add和update中,因为即便是update,那所指的配置项也只有一条

                    //新端口允许使用的逻辑
                    String serverPorts=serverService.queryServerById(service.getServerId()).getPorts();

                    if(serverPorts.contains(httpPort)||serverPorts.contains(shutdownPort)){//万一是无损更新呢,只需对比一下,老端口和新端口是否一样.一样就过,不一样就处理
                        if(tomcatAndHeartbeatServiceConfig.getHttpPort().equals(httpPort) && tomcatAndHeartbeatServiceConfig.getShutdownPort().equals(shutdownPort)){
                            //System.out.println("更新前和更新后,端口号一样,不冲突,可以继续更新");
                        }else{
                            //System.out.println("更新前和更新后,端口号不一样,说明:新端口号是别的服务在占用,但又不是本服务在用.因此,产生冲突.所以必须停止继续更新");
                            //那万一是服务内部互换,则不必视作冲突
                            if(tomcatAndHeartbeatServiceConfig.getHttpPort().equals(shutdownPort) && tomcatAndHeartbeatServiceConfig.getShutdownPort().equals(httpPort)){
                                //这便是服务内部端口互换,不视为冲突,可以继续更新
                            }else{
                                //否则,就不是服务内部端口互换,那就有可能是某一个端口被包含在了server使用中,或是全部包含在了server使用列表中.则不能继续更新,需要去查验
                                //response.getWriter().write("端口冲突,请查验 httpPort:"+httpPort+" ,shutdownPort: "+shutdownPort+" 到底哪个端口号冲突了");
                                //return;
                                String result="";//对比完后,结果仍为空,则包含,而不算冲突
                                if(serverPorts.contains(httpPort)){
                                    // 被包含的是 httpPort
                                    if (tomcatAndHeartbeatServiceConfig.getHttpPort().equals(httpPort)){
                                        //httpPort被包含,但与此前一致,过
                                    }else {
                                        //否则,就是 包含,但不一致,则为冲突
                                        result=result+"httpPort已被使用:"+httpPort;
                                    }
                                }else {
                                    // 看来被包含的不是 httpPort,httpPort可以启用
                                }
                                if(serverPorts.contains(shutdownPort)){
                                    // 被包含的是 shutdownPort
                                    if (tomcatAndHeartbeatServiceConfig.getShutdownPort().equals(shutdownPort)){
                                        //shutdownPort 被包含,但与此前一致,过
                                    }else {
                                        //否则,就是 包含,但不一致,则为冲突
                                        if (result.equals("")){
                                            result="shutdownPort已被使用:"+shutdownPort;
                                        }else {
                                            result=result+",shutdownPort已被使用:"+shutdownPort;
                                        }
                                    }
                                }else {
                                    // 看来被包含的不是 shutdownPort,shutdownPort 可以启用
                                }
                                if(result.equals("")){
                                    //结果为空,因此为包含,但前后一致,过
                                }else {
                                    //结果不为空,则返回用户,呈现异常
                                    response.getWriter().write(result);
                                    return;
                                }
                                //无论如何,这是serverPort包含了要提交的端口号,目的是挑出产生冲突的那个端口.亦或,端口跟此前的一样,就不算冲突了
                            }
                        }
                    }else{
                        //System.out.println("服务器上没有使用这个端口号,可以使用");
                    }//整台服务器不能有与之冲突的端口号记录存在
                    //新端口允许使用的逻辑-完
                    //老端口要不要释放的逻辑
                    if(tomcatAndHeartbeatServiceConfig.getHttpPort().equals(httpPort) && tomcatAndHeartbeatServiceConfig.getShutdownPort().equals(shutdownPort)){
                        //System.out.println("新老端口一样,不必释放老端口");
                    }else {
                        if (tomcatAndHeartbeatServiceConfig.getHttpPort().equals(shutdownPort) && tomcatAndHeartbeatServiceConfig.getShutdownPort().equals(httpPort)){
                            //此为服务内部端口互换,所以不必清理端口占用情况
                        }else {
                            //System.out.println("端口占用前后不一致,又不是内部互换,则必然有一方或多方是新用,那么必然有一方或多方是弃用,因此要全部放弃,后而加上");
                            String newServicePorts=WebUtils.reducePortItem(service.getPorts(),tomcatAndHeartbeatServiceConfig.getHttpPort());
                            newServicePorts=WebUtils.reducePortItem(newServicePorts,tomcatAndHeartbeatServiceConfig.getShutdownPort()); //本服务有2个端口,故而需要连续减除2次
                            service.setPorts(newServicePorts);
                            serviceService.updateService(service);
                        }

                    }
                    //老端口要不要释放的逻辑-完
                    tomcatAndHeartbeatServiceConfig.setTomcatHome(tomcatHome);
                    tomcatAndHeartbeatServiceConfig.setJavaHome(javaHome);
                    tomcatAndHeartbeatServiceConfig.setMaxMemory(maxMemory);
                    tomcatAndHeartbeatServiceConfig.setHttpPort(httpPort);
                    tomcatAndHeartbeatServiceConfig.setShutdownPort(shutdownPort);
                    tomcatAndHeartbeatServiceConfig.setProjectId(projectId);
                    tomcatAndHeartbeatServiceConfig.setModuleId(moduleId);
                    tomcatAndHeartbeatServiceConfig.setUse(use);
                    tomcatAndHeartbeatServiceConfig.setHeartbeat(heartbeat);

                    tomcatAndHeartbeatServiceConfigService.updateTomcatAndHeartbeatServiceConfig(tomcatAndHeartbeatServiceConfig);
                    String ports=WebUtils.addPortsItem(service.getPorts(),httpPort);
                    ports=WebUtils.addPortsItem(ports,shutdownPort);
                    service.setPorts(ports);
                    serviceService.updateService(service);

                    response.getWriter().write("存入成功");
                }//找得到-更新,找不到-添加
            }

            if(configType.equals("nginxServiceConfigTcpProxy")){
                String id=request.getParameter("id");
                System.out.println("这是 ServiceServlet.ajaxConfigService 的提示,id 是: "+id);
                NginxServiceConfigTcpProxy nginxServiceConfigTcpProxy=nginxServiceConfigTcpProxyService.queryNginxServiceConfigTcpProxyById(id);

                if (nginxServiceConfigTcpProxy!=null){
                    if(request.getParameter("delete")!=null){
                        if(request.getParameter("delete").equals("true")){//那么这就是要删除
                            WebUtils.deleteNginxServiceConfigTcpProxy(nginxServiceConfigTcpProxy);//这个方法中包含有递归删除port占用的方法
                            response.getWriter().write("删除成功");
                            return;
                        }
                    }
                }//删除的逻辑

                String front=request.getParameter("front");
                String end=request.getParameter("end");
                String use=request.getParameter("use");

                if(front==null|end==null|use==null|Objects.equals(front, "") | Objects.equals(end, "") |Objects.equals(use, "")){
                    response.getWriter().write("传参有问题");
                    return;
                }//传参判断正常与否

                String frontDomain="",frontPort="",endAddress="",endPort="";
                if(front.contains(":")){
                    frontDomain=front.split(":")[0];
                    frontPort=front.split(":")[1];
                }else{
                    response.getWriter().write("前端端口有问题");
                    return;
                }//判断front和end中是否有冒号,从而要不要进行分割
                if(end.contains(":")){
                    endAddress=end.split(":")[0];
                    endPort=end.split(":")[1];
                }else{
                    response.getWriter().write("后端端口有问题");
                    return;
                }//判断front和end中是否有冒号,从而要不要进行分割

                if(nginxServiceConfigTcpProxy==null){//找不到这个id，那么就是add
                    String serviceId=request.getParameter("serviceId");//serviceId 只会出现在add,update没有;update有的是:nginxServieConfigTcpProxyId
                    Service service=serviceService.queryServiceById(serviceId);
                    if(service==null){
                        response.getWriter().write("新增加tcpProxy时,serviceId有问题:"+serviceId);
                    }else {
                        //新端口允许使用的逻辑
                        String serverPorts=serverService.queryServerById(service.getServerId()).getPorts();
                        if(serverPorts.contains(frontPort)){//这是新增,所以不存在无损更新的情况;所以,要放入的端口,一定不能出现在serverPorts中
                            response.getWriter().write("新增加tcpProxy时,端口冲突");
                            return;
                        }else{
                            //System.out.println("服务器上没有使用这个端口号,可以使用");
                        }
                        //新端口允许使用的逻辑-完
                        //老端口要不要释放的逻辑
                            //add没有释放老端口这么一说
                        //老端口要不要释放的逻辑-完
                    }//整台服务器不能有与之冲突的端口号记录存在
                    Server server=serverService.queryServerById(service.getServerId());

                    NginxServiceConfigTcpProxy nginxServiceConfigTcpProxyAdd=new NginxServiceConfigTcpProxy();
                    nginxServiceConfigTcpProxyAdd.setId(WebUtils.id());
                    nginxServiceConfigTcpProxyAdd.setServerId(server.getId());
                    nginxServiceConfigTcpProxyAdd.setServiceId(service.getId());
                    nginxServiceConfigTcpProxyAdd.setFrontDomain(frontDomain);
                    nginxServiceConfigTcpProxyAdd.setFrontPort(frontPort);
                    nginxServiceConfigTcpProxyAdd.setEndAddress(endAddress);
                    nginxServiceConfigTcpProxyAdd.setEndPort(endPort);
                    nginxServiceConfigTcpProxyAdd.setUse(use);

                    nginxServiceConfigTcpProxyService.addNginxServiceConfigTcpProxy(nginxServiceConfigTcpProxyAdd);

                    service.setPorts(WebUtils.addPortsItem(service.getPorts(),frontPort));
                    serviceService.updateService(service);

                    response.getWriter().write("存入成功");
                }else {//找得到这个nginx tcpProxy id,那就是update
                    String serviceId=nginxServiceConfigTcpProxy.getServiceId();//serviceId 只会出现在add,update没有;update有的是:nginxServieConfigTcpProxyId
                    Service service=serviceService.queryServiceById(serviceId);

                    //新端口允许使用的逻辑
                    String serverPorts=serverService.queryServerById(service.getServerId()).getPorts();
                    if(serverPorts.contains(frontPort)){//万一是无损更新呢
                        if(nginxServiceConfigTcpProxy.getFrontPort().equals(front)){
                            //System.out.println("更新前和更新后,端口号一样,不冲突,可以继续更新");
                        }else{
                            //System.out.println("更新前和更新后,端口号不一样,说明:新端口号是别的服务在占用.因此,产生冲突.所以必须停止继续更新");
                            response.getWriter().write("端口冲突");
                            return;
                        }
                    }else{
                        //System.out.println("服务器上没有使用这个端口号,可以使用");
                    }//整台服务器不能有与之冲突的端口号记录存在
                    //新端口允许使用的逻辑-完
                    //老端口要不要释放的逻辑
                    if(nginxServiceConfigTcpProxy.getFrontPort().equals(frontPort)){
                        //System.out.println("新老端口一样,不必释放老端口");
                    }else {
                        //System.out.println("这是tcpProxy,不用了,一定不会有复用的情况,所以应该释放");
                        String newServicePorts=WebUtils.reducePortItem(service.getPorts(),nginxServiceConfigTcpProxy.getFrontPort());
                        service.setPorts(newServicePorts);
                        serviceService.updateService(service);
                    }
                    //老端口要不要释放的逻辑-完
                    nginxServiceConfigTcpProxy.setFrontDomain(frontDomain);
                    nginxServiceConfigTcpProxy.setFrontPort(frontPort);
                    nginxServiceConfigTcpProxy.setEndAddress(endAddress);
                    nginxServiceConfigTcpProxy.setEndPort(endPort);
                    nginxServiceConfigTcpProxy.setUse(use);
                    nginxServiceConfigTcpProxyService.updateNginxServiceConfigTcpProxy(nginxServiceConfigTcpProxy);

                    service.setPorts(WebUtils.addPortsItem(service.getPorts(),frontPort));
                    serviceService.updateService(service);

                    response.getWriter().write("存入成功");
                }//找得到-更新,找不到-添加
            }
            if(configType.equals("nginxServiceConfigPhp")){
                String id=request.getParameter("id");
                NginxServiceConfigPhp nginxServiceConfigPhp=nginxServiceConfigPhpService.queryNginxServiceConfigPhpById(id);

                if (nginxServiceConfigPhp!=null){
                    if(request.getParameter("delete")!=null){
                        if(request.getParameter("delete").equals("true")){//那么这就是要删除
                            WebUtils.deleteNginxServiceConfigPhp(nginxServiceConfigPhp);//这个方法中包含有递归删除port占用的方法
                            response.getWriter().write("删除成功");
                            return;
                        }
                    }
                }//删除一条的逻辑

                String domainPort=request.getParameter("domainPort");
                String workDirectory=request.getParameter("workDirectory");
                String use=request.getParameter("use");
                String projectId= request.getParameter("projectId");
                String projectModuleId= request.getParameter("projectModuleId");

                if(domainPort==null){
                    response.getWriter().write("传参有问题");
                    return;
                }//确定传参正常的逻辑
                String domain="",port="";
                if(domainPort.contains(":")){
                    domain=domainPort.split(":")[0];
                    port=domainPort.split(":")[1];
                }else{
                    domain=domainPort;
                    port="80";
                }//判断domainPort中是否有冒号,从而要不要进行分割

                if(nginxServiceConfigPhp==null){//查无次php,则为add
                    String serviceId=request.getParameter("serviceId");//serviceId只会出现在add,update没有;update有的是:nginxServieConfigPhpId
                    Service service=serviceService.queryServiceById(serviceId);

                    if(service==null){
                        response.getWriter().write("serviceId有问题:"+serviceId);
                        return;
                    }else {
                        //新端口允许使用的逻辑
                        String serverPorts=serverService.queryServerById(service.getServerId()).getPorts();
                        if(serverPorts.contains(port)){//如果服务器已经有这个端口号了,那么就要看,是不是php或proxy使用的了
                            //服务器上有这个端口号,那么这个端口号是nginx再用,还是别的服务在用?
                            String servicePorts=service.getPorts();
                            if(servicePorts.contains(port)){//如果service包含着这个端口号,那么要看这个端口号是被tcp占用了,还是被php和proxy占用了
                                List<NginxServiceConfigTcpProxy> nginxServiceConfigTcpProxies=nginxServiceConfigTcpProxyService.queryNginxServiceConfigTcpProxiesByFrontPort(port);
                                if(nginxServiceConfigTcpProxies.size()>0){//现在的情况是:服务器上有这个端口,且是本service占用的,且是tcp服务占用的.所以,这个新端口不能复用了.必须停止更新
                                    response.getWriter().write("现在的情况是:服务器上有这个端口,且是本service占用的,且是tcp服务占用的.所以,这个新端口不能复用了.必须停止更新");
                                    return;
                                }else{//现在的情况是:服务器上有这个端口,且是本service占用的,且是tcp服务没有占用的.所以,这个新端口可以复用.
                                    //System.out.println("现在的情况是:服务器上有这个端口,且是本service占用的,且是tcp服务没有占用的.所以,这个新端口可以复用."); //复用
                                }
                            }else{//service 没有占用这个端口号,但是server占用了;说明,有别的服务已经再用这个端口号了.所以要停止更新
                                response.getWriter().write("端口冲突:service 没有占用这个端口号,但是server占用了;说明,有别的服务已经再用这个端口号了.所以要停止更新");
                                return;
                            }
                        }else{//如果server不包含这个端口号,那么就可以放心大胆的更新了
                            //System.out.println("新启用");
                        }
                        //新端口允许使用的逻辑-完
                        //老端口要不要释放的逻辑
                            //这是add,没有老端口这么一说
                        //老端口要不要释放的逻辑-完
                    }//php,反向代理,同serviceid,同域名,同端口号,只能有一个解析记录
                    Server server=serverService.queryServerById(service.getServerId());

                    NginxServiceConfigPhp nginxServiceConfigPhpAdd=new NginxServiceConfigPhp();
                    nginxServiceConfigPhpAdd.setId(WebUtils.id());
                    nginxServiceConfigPhpAdd.setServerId(server.getId());
                    nginxServiceConfigPhpAdd.setServiceId(service.getId());
                    nginxServiceConfigPhpAdd.setDomain(domain);
                    nginxServiceConfigPhpAdd.setPort(port);
                    nginxServiceConfigPhpAdd.setWorkDirectory(workDirectory);
                    nginxServiceConfigPhpAdd.setUse(use);
                    nginxServiceConfigPhpAdd.setProjectId(projectId);
                    nginxServiceConfigPhpAdd.setProjectModuleId(projectModuleId);

                    nginxServiceConfigPhpService.addNginxServiceConfigPhp(nginxServiceConfigPhpAdd);

                    service.setPorts(WebUtils.addPortsItem(service.getPorts(),port));
                    serviceService.updateService(service);

                    response.getWriter().write("存入成功");
                }else {//查到就是update
                    String serviceId=nginxServiceConfigPhp.getServiceId();//serviceId只会出现在add,update没有;update有的是:nginxServieConfigPhpId
                    Service service=serviceService.queryServiceById(serviceId);

                    //新端口允许使用的逻辑
                    String serverPorts=serverService.queryServerById(service.getServerId()).getPorts();
                    if(serverPorts.contains(port)){//如果服务器已经有这个端口号了,那么就要看,是不是php或proxy使用的了
                        //服务器上有这个端口号,那么这个端口号是nginx再用,还是别的服务在用?
                        String servicePorts=service.getPorts();
                        if(servicePorts.contains(port)){//如果service包含着这个端口号,那么要看这个端口号是被tcp占用了,还是被php和proxy占用了
                            List<NginxServiceConfigTcpProxy> nginxServiceConfigTcpProxies=nginxServiceConfigTcpProxyService.queryNginxServiceConfigTcpProxiesByFrontPort(port);
                            if(nginxServiceConfigTcpProxies.size()>0){//现在的情况是:服务器上有这个端口,且是本service占用的,且是tcp服务占用的.所以,这个新端口不能复用了.必须停止更新
                                response.getWriter().write("现在的情况是:服务器上有这个端口,且是本service占用的,且是tcp服务占用的.所以,这个新端口不能复用了.必须停止更新");
                                return;
                            }else{//现在的情况是:服务器上有这个端口,且是本service占用的,且是tcp服务没有占用的.所以,这个新端口可以复用.
                                //System.out.println("现在的情况是:服务器上有这个端口,且是本service占用的,且是tcp服务没有占用的.所以,这个新端口可以复用."); //复用
                            }
                        }else{//service 没有占用这个端口号,但是server占用了;说明,有别的服务已经再用这个端口号了.所以要停止更新
                            response.getWriter().write("端口冲突:service 没有占用这个端口号,但是server占用了;说明,有别的服务已经再用这个端口号了.所以要停止更新");
                            return;
                        }
                    }else{//如果server不包含这个端口号,那么就可以放心大胆的更新了
                        //System.out.println("新启用");
                    }
                    //新端口允许使用的逻辑-完
                    //老端口要不要释放的逻辑
                    if(nginxServiceConfigPhp.getPort().equals(port)){
                        //System.out.println("新老端口一样,不必释放老端口");
                    }else {
                        System.out.println("新老端口不一样,要看看老端口在php中是否为1,在proxy中是否为0条");
                        List<NginxServiceConfigPhp> nginxServiceConfigPhps=nginxServiceConfigPhpService.queryNginxServiceConfigPhpsByPort(nginxServiceConfigPhp.getPort());
                        List<NginxServiceConfigProxy> nginxServiceConfigProxies=nginxServiceConfigProxyService.queryNginxServiceConfigProxiesByFrontPort(nginxServiceConfigPhp.getPort());
                        if(nginxServiceConfigPhps.size()==1 & nginxServiceConfigProxies.size()==0 & nginxServiceConfigPhp.getPort().equals("80") & nginxServiceConfigPhp.getPort().equals("443")){
                            //如果php就1条,proxy没有记录,那么就该放弃了,除非是80,不放弃
                            String newServicePorts=WebUtils.reducePortItem(service.getPorts(),nginxServiceConfigPhp.getPort());
                            service.setPorts(newServicePorts);
                            serviceService.updateService(service);
                        }

                        //if(nginxServiceConfigPhps.size()>1 & nginxServiceConfigProxies.size()>0){
                        //    //System.out.println("php大于1,proxy大于0条,说明这个端口需要被复用,因此不可以释放");
                        //}else {
                        //    //System.out.println("php不大于1,proxy大于0");//在这个情况下,老端口将被proxy占用
                        //    //System.out.println("php不大于1,proxy等于0");//在这个情况下,老端口即将被php释放,也不会被proxy占用,所以应该放弃
                        //    //System.out.println("php大于1,proxy等于0");//在这个情况下,老端口将被php占用
                        //    //System.out.println("php大于1,proxy大于0");//不会进入这里
                        //}
                    }
                    //老端口要不要释放的逻辑-完
                    nginxServiceConfigPhp.setDomain(domain);
                    nginxServiceConfigPhp.setPort(port);
                    nginxServiceConfigPhp.setWorkDirectory(workDirectory);
                    nginxServiceConfigPhp.setUse(use);
                    nginxServiceConfigPhp.setProjectId(projectId);
                    nginxServiceConfigPhp.setProjectModuleId(projectModuleId);
                    nginxServiceConfigPhpService.updateNginxServiceConfigPhp(nginxServiceConfigPhp);

                    service.setPorts(WebUtils.addPortsItem(service.getPorts(),port));
                    serviceService.updateService(service);

                    response.getWriter().write("存入成功");
                }//找得到-更新,找不到-添加
            }
            if(configType.equals("nginxServiceConfigProxy")){
                String id=request.getParameter("id");
                NginxServiceConfigProxy nginxServiceConfigProxy=nginxServiceConfigProxyService.queryNginxServiceConfigProxyById(id);

                if (nginxServiceConfigProxy!=null){
                    if(request.getParameter("delete")!=null){
                        if(request.getParameter("delete").equals("true")){//那么这就是要删除
                            WebUtils.deleteNginxServiceConfigProxy(nginxServiceConfigProxy);//这个方法中包含有递归删除port占用的方法
                            response.getWriter().write("删除成功");
                            return;
                        }
                    }
                }

                String front=request.getParameter("front");
                String projectModuleTypeId=request.getParameter("projectModuleTypeId");
                String projectId=request.getParameter("projectId");
                String projectModuleId=request.getParameter("projectModuleId");
                String targetNodeId=request.getParameter("targetNodeId");
                String use=request.getParameter("use");

                if(front==null){
                    response.getWriter().write("传参有问题");
                    return;
                }//传参有问题

                String frontDomain="",frontPort="";
                if(front.contains(":")){
                    frontDomain=front.split(":")[0];
                    frontPort=front.split(":")[1];
                }else{
                    frontDomain=front;
                    frontPort="80";
                }//判断front和end中是否有冒号,从而要不要进行分割

                if(nginxServiceConfigProxy==null){//找不到这个id，那么就是add
                    String serviceId=request.getParameter("serviceId");//serviceId 只会出现在add,update没有;update有的是:nginxServieConfigProxyId
                    Service service=serviceService.queryServiceById(serviceId);
                    if(service==null){
                        response.getWriter().write("serviceId有问题:"+serviceId);
                    }else {
                        //新端口允许使用的逻辑
                        String serverPorts=serverService.queryServerById(service.getServerId()).getPorts();
                        if(serverPorts.contains(frontPort)){//如果服务器已经有这个端口号了,那么就要看,是不是php或proxy使用的了
                            //服务器上有这个端口号,那么这个端口号是nginx再用,还是别的服务在用?
                            String servicePorts=service.getPorts();
                            if(servicePorts.contains(frontPort)){//如果service包含着这个端口号,那么要看这个端口号是被tcp占用了,还是被php和proxy占用了
                                List<NginxServiceConfigTcpProxy> nginxServiceConfigTcpProxies=nginxServiceConfigTcpProxyService.queryNginxServiceConfigTcpProxiesByFrontPort(frontPort);
                                if(nginxServiceConfigTcpProxies.size()>0){//现在的情况是:服务器上有这个端口,且是本service占用的,且是tcp服务占用的.所以,这个新端口不能复用了.必须停止更新
                                    response.getWriter().write("现在的情况是:服务器上有这个端口,且是本service占用的,且是tcp服务占用的.所以,这个新端口不能复用了.必须停止更新");
                                    return;
                                }else{//现在的情况是:服务器上有这个端口,且是本service占用的,且是tcp服务没有占用的.所以,这个新端口可以复用.
                                    //System.out.println("现在的情况是:服务器上有这个端口,且是本service占用的,且是tcp服务没有占用的.所以,这个新端口可以复用."); //复用
                                }
                            }else{//service 没有占用这个端口号,但是server占用了;说明,有别的服务已经再用这个端口号了.所以要停止更新
                                response.getWriter().write("端口冲突");
                                return;
                            }
                        }else{//如果server不包含这个端口号,那么就可以放心大胆的更新了
                            //System.out.println("新启用");
                        }
                        //新端口允许使用的逻辑-完
                    }//php,反向代理,同serviceid,同域名,同端口号,只能有一个解析记录
                    Server server=serverService.queryServerById(service.getServerId());

                    NginxServiceConfigProxy nginxServiceConfigProxyAdd=new NginxServiceConfigProxy();
                    nginxServiceConfigProxyAdd.setId(WebUtils.id());
                    nginxServiceConfigProxyAdd.setServerId(server.getId());
                    nginxServiceConfigProxyAdd.setServiceId(service.getId());
                    nginxServiceConfigProxyAdd.setFrontDomain(frontDomain);
                    nginxServiceConfigProxyAdd.setFrontPort(frontPort);
                    nginxServiceConfigProxyAdd.setProjectModuleTypeId(projectModuleTypeId);
                    nginxServiceConfigProxyAdd.setProjectId(projectId);
                    nginxServiceConfigProxyAdd.setProjectModuleId(projectModuleId);
                    nginxServiceConfigProxyAdd.setTargetNodeId(targetNodeId);
                    nginxServiceConfigProxyAdd.setUse(use);

                    nginxServiceConfigProxyService.addNginxServiceConfigProxy(nginxServiceConfigProxyAdd);

                    service.setPorts(WebUtils.addPortsItem(service.getPorts(),frontPort));
                    serviceService.updateService(service);

                    response.getWriter().write("存入成功");
                }else {//找得到这个nginx proxy id,那就是update
                    String serviceId=nginxServiceConfigProxy.getServiceId();//serviceId 只会出现在add,update没有;update有的是:nginxServieConfigProxyId
                    Service service=serviceService.queryServiceById(serviceId);
                    //新端口允许使用的逻辑
                    String serverPorts=serverService.queryServerById(service.getServerId()).getPorts();
                    if(serverPorts.contains(frontPort)){//如果服务器已经有这个端口号了,那么就要看,是不是php或proxy使用的了
                        //服务器上有这个端口号,那么这个端口号是nginx再用,还是别的服务在用?
                        String servicePorts=service.getPorts();
                        if(servicePorts.contains(frontPort)){//如果service包含着这个端口号,那么要看这个端口号是被tcp占用了,还是被php和proxy占用了
                            List<NginxServiceConfigTcpProxy> nginxServiceConfigTcpProxies=nginxServiceConfigTcpProxyService.queryNginxServiceConfigTcpProxiesByFrontPort(frontPort);
                            if(nginxServiceConfigTcpProxies.size()>0){//现在的情况是:服务器上有这个端口,且是本service占用的,且是tcp服务占用的.所以,这个新端口不能复用了.必须停止更新
                                response.getWriter().write("现在的情况是:服务器上有这个端口,且是本service占用的,且是tcp服务占用的.所以,这个新端口不能复用了.必须停止更新");
                                return;
                            }else{//现在的情况是:服务器上有这个端口,且是本service占用的,且是tcp服务没有占用的.所以,这个新端口可以复用.
                                //System.out.println("现在的情况是:服务器上有这个端口,且是本service占用的,且是tcp服务没有占用的.所以,这个新端口可以复用."); //复用
                            }
                        }else{//service 没有占用这个端口号,但是server占用了;说明,有别的服务已经再用这个端口号了.所以要停止更新
                            response.getWriter().write("端口冲突");
                            return;
                        }
                    }else{//如果server不包含这个端口号,那么就可以放心大胆的更新了
                        //System.out.println("新启用");
                    }
                    //新端口允许使用的逻辑-完
                    //老端口要不要释放的逻辑
                    if(nginxServiceConfigProxy.getFrontPort().equals(frontPort)){
                        //System.out.println("新老端口一样,不必释放老端口");
                    }else {
                        //System.out.println("新老端口不一样,要看看老端口在php中是否为1,在proxy中是否为0条");
                        List<NginxServiceConfigPhp> nginxServiceConfigPhps=nginxServiceConfigPhpService.queryNginxServiceConfigPhpsByPort(nginxServiceConfigProxy.getFrontPort());
                        List<NginxServiceConfigProxy> nginxServiceConfigProxies=nginxServiceConfigProxyService.queryNginxServiceConfigProxiesByFrontPort(nginxServiceConfigProxy.getFrontPort());
                        if(nginxServiceConfigPhps.size()==1 & nginxServiceConfigProxies.size()==0 & nginxServiceConfigProxy.getFrontPort().equals("80") & nginxServiceConfigProxy.getFrontPort().equals("443")){
                            //如果php就1条,proxy没有记录,那么就该放弃了,除非是80,不放弃
                            String newServicePorts=WebUtils.reducePortItem(service.getPorts(),nginxServiceConfigProxy.getFrontPort());
                            service.setPorts(newServicePorts);
                            serviceService.updateService(service);
                        }

                        //if(nginxServiceConfigPhps.size()>1 & nginxServiceConfigProxies.size()>0){
                        //    //System.out.println("php大于1,proxy大于0条,说明这个端口需要被复用,因此不可以释放");
                        //}else {
                        //    //System.out.println("php不大于1,proxy大于0");//在这个情况下,老端口将被proxy占用
                        //    //System.out.println("php不大于1,proxy等于0");//在这个情况下,老端口即将被php释放,也不会被proxy占用,所以应该放弃
                        //    //System.out.println("php大于1,proxy等于0");//在这个情况下,老端口将被php占用
                        //    //System.out.println("php大于1,proxy大于0");//不会进入这里
                        //}
                    }
                    //老端口要不要释放的逻辑-完
                    nginxServiceConfigProxy.setFrontDomain(frontDomain);
                    nginxServiceConfigProxy.setFrontPort(frontPort);
                    nginxServiceConfigProxy.setProjectModuleTypeId(projectModuleTypeId);
                    nginxServiceConfigProxy.setProjectId(projectId);
                    nginxServiceConfigProxy.setProjectModuleId(projectModuleId);
                    nginxServiceConfigProxy.setTargetNodeId(targetNodeId);
                    nginxServiceConfigProxy.setUse(use);
                    nginxServiceConfigProxyService.updateNginxServiceConfigProxy(nginxServiceConfigProxy);

                    service.setPorts(WebUtils.addPortsItem(service.getPorts(),frontPort));
                    serviceService.updateService(service);

                    response.getWriter().write("存入成功");
                }//找得到-更新,找不到-添加
            }
        }//根据访问方式的不同,予以不同的处理方式,如果是get,那就给与配置服务的页面;如果是post,那就从参数中查询要配置内容
    }
    protected void getTargetNodesByProjectModuleId(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException{
        String projectModuleId=request.getParameter("projectModuleId");
        if (projectModuleId=="" || projectModuleId==null){
            response.getWriter().write("请提供模块id");
            return;
        }//检查模块id是否为空
        ProjectModule projectModule =projectModuleService.queryProjectModuleById(projectModuleId);
        if (projectModule==null){
            response.getWriter().write("projectModuleId 无效:"+projectModuleId);
            return;
        }//检查模块id无效的情况

        String projectModuleTypeName=projectModuleTypeService.queryProjectModuleTypeById(projectModule.getProjectModuleTypeId()).getName();
        if(projectModuleTypeName.equals("php")){
            List<NginxServiceConfigPhp> nginxServiceConfigPhps=nginxServiceConfigPhpService.queryNginxServiceConfigPhpsByProjectModuleId(projectModuleId);
            response.getWriter().write(gson.toJson(nginxServiceConfigPhps));
        }
        if(projectModuleTypeName.equals("tomcat")){
            List<TomcatServiceConfig> tomcatServiceConfigs=tomcatServiceConfigService.queryTomcatServiceConfigsByProjectModuleId(projectModuleId);
            response.getWriter().write(gson.toJson(tomcatServiceConfigs));
        }
        if(projectModuleTypeName.equals("tomcatAndHeartbeat")){
            List<TomcatAndHeartbeatServiceConfig> tomcatAndHeartbeatServiceConfigs=tomcatAndHeartbeatServiceConfigService.queryTomcatAndHeartbeatServiceConfigsByProjectModuleId(projectModuleId);
            response.getWriter().write(gson.toJson(tomcatAndHeartbeatServiceConfigs));
        }
    }
    protected void list(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException{
        String serverId=request.getParameter("serverId");

        System.out.println(Thread.currentThread().getStackTrace()[1].getFileName()+":"
                +Thread.currentThread().getStackTrace()[1].getLineNumber()+":"+"当前serverId是: "+serverId);
        List<Server> servers=serverService.queryServers();
        request.setAttribute("servers",servers);

        if(serverId==null){//参数中没有serverId,所以要显示全部的service
            List<Service> services= serviceService.queryServices();
            request.setAttribute("services",services);
            request.setAttribute("serverService",serverService);
            request.getRequestDispatcher("/pages/client/service.jsp").forward(request,response);
        }else{//参数中有 serverId,显示相关的service即可
            Server server=serverService.queryServerById(serverId);
            if(server==null){
                response.getWriter().write("找不到id为: "+serverId+" 的服务器");
                return;
            }
            request.setAttribute("server",server);
            List<Service> services= serviceService.queryServicesByServerId(serverId);
            request.setAttribute("services",services);
            request.setAttribute("serverService",serverService);
            request.setAttribute("serverName",server.getName());
            request.getRequestDispatcher("/pages/client/service.jsp").forward(request,response);
        }//参数中有serverId,显示特定服务器的服务;参数中没有 serverId ,显示全部server的服务

    }
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        System.out.println(Thread.currentThread().getStackTrace()[1].getFileName()+":"
                +Thread.currentThread().getStackTrace()[1].getLineNumber()+":"
                +"已经进入doget方法,"+request.getParameter("action")+" 是action名");
        doPost(request,response);//把dopost方法优化到baseServlet抽象类中去了,使得可以按照request action执行特定的方法
        //由于action判断行为在dopost中,所以在doget中执行一下dopost就可以执行判断了,从而执行特定的方法了.
    }
}
